package com.lej.share.ui

import android.content.*
import android.net.Uri
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.children
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import com.lej.share.R
import com.lej.wx.PlatformUtil
import com.lej.share.databinding.ShareFansDialogDefaultBinding
import com.qmuiteam.qmui.kotlin.onClick

class ShareFansBottomSheetDialog(private val context: AppCompatActivity, val text: String) :
    BottomPopupView(context) {
    override fun getImplLayoutId() = R.layout.share_fans_dialog_default

    lateinit var binding: ShareFansDialogDefaultBinding
    override fun onCreate() {
        super.onCreate()
        binding = ShareFansDialogDefaultBinding.bind(bottomPopupContainer.children.first())
        binding.tvCancel.onClick {
            dismiss()
        }
        binding.tvContent.text = text
        binding.icShareDy.onClick {
            if (!PlatformUtil.isDYClientAvailable(context)) {
                "抖音客户端未安装".toast()
                return@onClick
            }
            "snssdk1128://user/profile/111608789982".startUri()
        }
        binding.icShareKs.onClick {
            if (!PlatformUtil.isKSClientAvailable(context)) {
                "快手客户端未安装".toast()
                return@onClick
            }
            "kwai://profile/1683754934".startUri()
//            "https://v.kuaishou.com/81smbr"
        }
        binding.icShareXhs.onClick {
            if (!PlatformUtil.isXHSClientAvailable(context)) {
                "小红书客户端未安装".toast()
                return@onClick
            }
            "xhsdiscover://user/5cd12d8a0000000011008af2".startUri()
        }
        binding.icShareWeibo.onClick {
            if (!PlatformUtil.isWeiboClientAvailable(context)) {
                "微博客户端未安装".toast()
                return@onClick
            }
            "https://weibo.com/u/7418408557".startUri()
        }
    }

    companion object {
        fun shareText(context: AppCompatActivity, text: String) {
            XPopup.Builder(context).asCustom(ShareFansBottomSheetDialog(context, text)).show()
        }
    }

    fun CharSequence.toast() {
        Toast.makeText(context, this, Toast.LENGTH_LONG).show()
    }

    private fun String.startUri() {
        copyText()
        kotlin.runCatching {
            context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(this)))
        }
    }

    private fun copyText() {
        kotlin.runCatching {
            //复制功能
            val cm = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            cm.setPrimaryClip(ClipData.newPlainText(null, text))
        }
    }

//    private fun startKSLauncher() {
//        val intent = Intent(Intent.ACTION_MAIN)
//        val cmp = ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI")
//        intent.addCategory(Intent.CATEGORY_LAUNCHER)
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//        intent.component = cmp
//        context.startActivity(intent)
//    }
}
